﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetCoreApi.Plugins.Quartz
{
    /// <summary>
    /// 定时任务帮助类
    /// </summary>
    public class ScheduleJobHelper
    {
        private readonly ISchedulerFactory _schedulerFactory;
        private IScheduler scheduler;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="schedulerFactory"></param>
        public ScheduleJobHelper(ISchedulerFactory schedulerFactory)
        {
            _schedulerFactory = schedulerFactory;
        }
        private async Task Init()
        {
            //通过调度工厂获得调度器
            scheduler = await _schedulerFactory.GetScheduler();
            if (!scheduler.IsStarted)//调度器未启动
            {
                //开启调度器
                await scheduler.Start();
            }
        }
        /// <summary>
        /// 绑定方法
        /// </summary>
        /// <param name="jobDetail"></param>
        /// <param name="trigger"></param>
        /// <returns></returns>
        public async Task Bind(IJobDetail jobDetail,ITrigger trigger)
        {
            await Init();
            //将触发器和任务器绑定到调度器中
            await scheduler.ScheduleJob(jobDetail, trigger);
            
        }
    }
}
